﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UpdatedHomeMaker
{
    public partial class DayInformation : Form
    {
        public bool save;
        public DocumentedDay dayInfo;
        public DayInformation()
        {
            dayInfo = new DocumentedDay();
            InitializeComponent();
            comboBox1.DataSource = Enum.GetValues(typeof(PayStatus));
        }
        public DayInformation(DocumentedDay toEdit)
        {
            dayInfo = new DocumentedDay(toEdit);
            InitializeComponent();
            comboBox1.DataSource = Enum.GetValues(typeof(PayStatus));
            startTime.Value = dayInfo.StartTime;
            endTime.Value = dayInfo.EndTime;
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf(toEdit.Status);
            refreshTotalTime();
            refreshTotalUnits();
        }
        private void startTime_ValueChanged(object sender, EventArgs e)
        {
            dayInfo.StartTime = startTime.Value;
            refreshTotalTime();
            refreshTotalUnits();
        }

        private void endTime_ValueChanged(object sender, EventArgs e)
        {
            dayInfo.EndTime = endTime.Value;
            refreshTotalTime();
            refreshTotalUnits();
        }
        private void refreshTotalUnits()
        {
            int totalDays = endTime.Value.Day - startTime.Value.Day;
            int totalHours = endTime.Value.Hour - startTime.Value.Hour;
            int totalMin = endTime.Value.Minute - startTime.Value.Minute;
            totalHours += totalDays * 24;
            totalMin += totalHours * 60;
            int totalUnits = totalMin / 15;
            totalUnitsBox.Text = totalUnits.ToString();
            dayInfo.Units = Int32.Parse(totalUnitsBox.Text);
        }
        private void refreshTotalTime()
        {
            int totalDays = endTime.Value.Day - startTime.Value.Day;
            int totalHours = endTime.Value.Hour - startTime.Value.Hour;
            int totalMin = Math.Abs(endTime.Value.Minute - startTime.Value.Minute);
            if (startTime.Value.Minute > endTime.Value.Minute)
            {
                totalHours--;
                totalMin = 60 - Math.Abs(startTime.Value.Minute - endTime.Value.Minute);
            }
            totalHours += totalDays * 24;
            totalTimeText.Text = totalHours.ToString() + ":" + totalMin.ToString("00");
            dayInfo.HoursAndMin = totalTimeText.Text;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
                dayInfo.Status = (PayStatus)comboBox1.SelectedItem;
        }

        private void saveAndExit_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void exitNoSave_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
